<?php

namespace app\model\zs;

use app\model\v2\V2SystemUser;
use app\mytrait\ArrTrait;
use think\Model;

class ZsMemberArticle extends Model
{
    use ArrTrait;

    // 设置当前模型对应的完整数据表名称
    protected $table = 'member_articles';

    // 设置当前模型的数据库连接
    protected $connection = 'zs';

    // 自动写入时间的类型
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'create_at';
    protected $updateTime = 'update_at';

    const STATUS_ON = 1;
    const STATUS_OFF = 2;

    const AUDIT_STATUS_WAIT = 1;
    const AUDIT_STATUS_PASS = 2;
    const AUDIT_STATUS_FAIL = 3;

    /**
     * 状态对应文本
     */
    public static function statusRels()
    {
        return [
            static::STATUS_ON => '启用',
            static::STATUS_OFF => '禁用',
        ];
    }

    /**
     * 状态对应css class名称
     */
    public static function statusClassRels()
    {
        return [
            static::STATUS_ON => '',
            static::STATUS_OFF => 'color-red',
        ];
    }

    /**
     * 审核状态对应文本
     */
    public static function auditStatusRels()
    {
        return [
            static::AUDIT_STATUS_WAIT => '待审核',
            static::AUDIT_STATUS_PASS => '审核通过',
            static::AUDIT_STATUS_FAIL => '审核驳回',
        ];
    }

    /**
     * 审核状态对应css class名称
     */
    public static function auditStatusClassRels()
    {
        return [
            static::AUDIT_STATUS_WAIT => '',
            static::AUDIT_STATUS_PASS => 'color-green',
            static::AUDIT_STATUS_FAIL => 'color-red',
        ];
    }

    /**
     * 审核状态对应文本（小程序端）
     * - 审核通过状态不显示在小程序端
     */
    public static function auditStatusRelsMini()
    {
        return [
            static::AUDIT_STATUS_WAIT => '待审核',
            // static::AUDIT_STATUS_PASS => '审核通过',
            static::AUDIT_STATUS_FAIL => '审核驳回',
        ];
    }

    /**
     * 审核状态对应css class名称（小程序端）
     */
    public static function auditStatusClassRelsMini()
    {
        return [
            static::AUDIT_STATUS_WAIT => 'primary',
            static::AUDIT_STATUS_PASS => '',
            static::AUDIT_STATUS_FAIL => 'error',
        ];
    }

    public function relBusiness()
    {
        return $this->belongsTo(ZsBusiness::class, 'rel_business_id', 'id');
    }

    public function relTopic()
    {
        return $this->belongsTo(ZsMemberTopic::class, 'rel_topic_id', 'id');
    }

    public function relCoupon()
    {
        return $this->belongsTo(ZsMemberCoupon::class, 'rel_coupon_tid', 'tid');
    }

    public function memberUser()
    {
        return $this->belongsTo(ZsMemberMeal::class, 'vipcode', 'vipcode');
    }

    // TODO: 跨库进行关联查询可能有问题，待确定
    public function auditUser()
    {
        return $this->belongsTo(V2SystemUser::class, 'audit_user_id', 'id');
    }

    public function getMediaArrAttr($value)
    {
        $item_arr = is_array($value) ? $value : array_filter(json_decode($value, true) ?: []);
        $item_arr = count($item_arr) === 1 ? explode(',', $item_arr[0]) : $item_arr;
        $item_arr = array_values(array_filter($item_arr));
        return $item_arr;
    }

    public function setMediaArrAttr($value)
    {
        $arr = $this->arrQuote($value);
        return $arr ? json_encode($arr, JSON_UNESCAPED_UNICODE) : '';
    }

    // 所有评论
    public function commentsModel()
    {
        return $this->hasMany(ZsMemberArticleComment::class, 'article_id', 'id');
    }

    // 所有待审核评论
    public function commentsWaitModel()
    {
        return $this->hasMany(ZsMemberArticleComment::class, 'article_id', 'id')
            ->where('audit_status', ZsMemberArticleComment::AUDIT_STATUS_WAIT);
    }
}
